package com.zzmyjjxy.dao;

import com.zzmyjjxy.entity.SharedBike;
import com.zzmyjjxy.entity.SharedBikeCompany;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SharedBikeDao {

    private SharedBikeCompany[] companies ;

    public void init(){
        companies = new SharedBikeCompany[]{
                new SharedBikeCompany(1,"ofo单车",new SharedBike[]{
                        new SharedBike(1,"ofo单车",0,"")
                },1,0),
                new SharedBikeCompany(2,"halo单车",new SharedBike[]{
                        new SharedBike(1,"halo单车",0,"")
                },1,0),
                new SharedBikeCompany(3,"mobai单车",new SharedBike[]{
                        new SharedBike(1,"mobai单车",0,"")
                },1,0)
        };
    }
    public SharedBikeCompany[] getCompanies(){
        return companies;
    }
    /**
     * 冒泡排序
     * @return 公司数组
     */
    public SharedBikeCompany[] rankOfBike(){
        SharedBikeCompany[] s = getCompanies();
        for (int i = 0; i < s.length-1; i++) {
            for (int j = 0; j < s.length - i - 1; j++) {
                if(s[j].getCount() < s[j+1].getCount()){
                    SharedBikeCompany temp  = s[j];
                    s[j] = s[j+1];
                    s[j+1] = temp;
                }
            }
        }
        return s;
    }

    public Integer getMaxBikeNo(SharedBikeCompany company){
        int max = 0;
        for (SharedBike sharedBike : company.getSharedBikes()) {
            if( sharedBike  != null && sharedBike.getBid() > max){
                max = sharedBike.getBid();
            }
        }
        int bid = max + 1;
        return bid;
    }
    public SharedBike borrowOrReturnBike(int flag, SharedBikeCompany company, int bikeNo,String timeStr){// 0 借出  1 还回
        for (int i = 0; i < company.getSharedBikes().length; i++) {
            if(company.getSharedBikes()[i] != null && company.getSharedBikes()[i].getBid() == bikeNo){
                if(flag == 0){
                    if(company.getSharedBikes()[i].getStatus() == 1){
                        throw new RuntimeException("已借出的单车不可再借出");
                    }
                    try {
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr);
                    } catch (ParseException e) {
                        throw new RuntimeException("时间格式错误");
                    }
                    company.getSharedBikes()[i].setStatus(1);
                    company.getSharedBikes()[i].setBorrowTime(timeStr);
                    company.setCount(company.getCount() + 1);
                }else{
                    if(company.getSharedBikes()[i].getStatus() == 0){
                        throw new RuntimeException("你没有借出该单车，不可归还");
                    }
                    try {
                        Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr);
                        Date borrowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(company.getSharedBikes()[i].getBorrowTime());
                        if(now.getTime() <= borrowTime.getTime()){
                            throw new RuntimeException("归还时间不能早于借出时间");
                        }
                        long mills = now.getTime() - borrowTime.getTime();
                        long hour = Math.round(Math.ceil(mills / (1000 * 60 * 60.0)));
                        System.out.println("一共花费："+(hour * 2)+"元");
                    } catch (ParseException e) {
                        throw new RuntimeException("时间格式错误");
                    }
                    company.getSharedBikes()[i].setStatus(0);
                }
                return company.getSharedBikes()[i];
            }
        }
        return null;
    }

    public SharedBikeCompany getCompanyByNo(int companyNo){
        SharedBikeCompany company = null;
        //先查到对应的公司
        for (int i = 0; i < companies.length; i++) {
            if(companies[i].getBikeCompanyId() == companyNo){
                company = companies[i];
                break;
            }
        }
        return company;
    }

    public void deleteBike(SharedBikeCompany company, int bikeNo){
        //找到对应bike 删除
        for (int i = 0; i < company.getSharedBikes().length; i++) {
            if(company.getSharedBikes()[i] != null && company.getSharedBikes()[i].getBid() == bikeNo){
                if(company.getSharedBikes()[i].getStatus() == 1){
                    throw new RuntimeException("已借出的单车不可删除");
                }
                company.getSharedBikes()[i] = null;
                company.setSum(company.getSum() - 1);
                break;
            }
        }
    }

    public void addBike(SharedBikeCompany company , SharedBike bike){
        //添加bike 如果空间足够
        boolean flag = false;
        for (int i = 0; i < company.getSharedBikes().length; i++) {
            if(company.getSharedBikes()[i] == null){
                company.getSharedBikes()[i] = bike;
                flag = true;
                break;
            }
        }
        //空间不够 先扩容  再添加bike
        if(!flag){
            System.out.println("空间不足，先进行扩容操作，扩容后容量为："+(company.getSharedBikes().length * 2));
            SharedBike[] bikes = new SharedBike[company.getSharedBikes().length * 2];
            for (int i = 0; i < company.getSharedBikes().length; i++) {
                bikes[i] = company.getSharedBikes()[i];
            }
            company.setSharedBikes(bikes);

            for (int i = 0; i < company.getSharedBikes().length; i++) {
                if(company.getSharedBikes()[i] == null){
                    company.getSharedBikes()[i] = bike;
                    break;
                }
            }
        }
    }

}
